package com.dhcc.sds.busi.controller;

import com.alibaba.fastjson.JSONArray;
import com.dhcc.cdc.busi.service.ICdcHospitalService;
import com.dhcc.core.config.properties.SysProperties;
import com.dhcc.core.framework.result.R;
import com.dhcc.core.framework.util.RedisUtil;
import com.dhcc.sds.base.service.ISdsCommonService;
import com.dhcc.sds.busi.service.IAnalysisReportService;
import freemarker.template.TemplateException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 分析报告
 * @File Name: AnalysisReportController
 * @author: zhangdc
 * @date: 2022年10月10日 上午9时15分0秒
 */
@Controller
@RequestMapping("${sys.backendPath}/analysis")
public class AnalysisReportController {

    private String PREFIX = "/backend/sds/report/";

    @Autowired
    private IAnalysisReportService analysisReportService;
    @Autowired
    private SysProperties sysPro;
    @Autowired
    private ICdcHospitalService cdcHospitalService;
    @Autowired
    private ISdsCommonService sdsCommonService;

    /**
     * 跳转到分析报告
     */
    @RequestMapping("/report")
    public String report(Model model) {
        //获取部门信息
        JSONArray officMap = cdcHospitalService.getOfficeMap();
        //加载用户关联科室
        JSONArray departMap = sdsCommonService.getDepartMap(new HashMap<>());
        model.addAttribute("officeMap", officMap);
        model.addAttribute("departMap", departMap);
        model.addAttribute("hospitalId", sdsCommonService.getHospIdBySysUser());
        return PREFIX + "report.html";
    }

    /**
     *
     * @return filaName
     */
    @RequestMapping(value="/creatReport")
    @ResponseBody
    public Object creatReport(@RequestParam Map<String ,Object > params) throws Exception {
        String filaName = analysisReportService.creatReport(params);
        return R.ok(filaName);
    }

    /**
     * 执行报告导出进度
     * @return
     */
    @RequestMapping("/getExportProcess")
    @ResponseBody
    public Object getExportProcess() {
        return R.ok(analysisReportService.getExportProcess());
    }

    /**
     * @Description: 分析报告下载
     * @Title: downFile
     * @param resp
     * @param req
     * @throws IOException
     * @return: void
     * @throws
     * @author: zhangdc
     * @date:2022年10月11日 下午3:00:45
     */
    @RequestMapping(value="/downFile/{fileName:.+}")
    @ResponseBody
    public void downFile(@PathVariable String fileName, HttpServletResponse resp, HttpServletRequest req) throws IOException{
        //找到在服务器的目标位置
        String path = sysPro.getStaticPath()+"file_getReport/reportFiles/"+fileName;
        File file = new File(path);
        if(file.exists()){
            //防止中文乱码
            byte [] tmpb=fileName.getBytes("GB2312");
            String fileNameCN = new String(tmpb, "8859_1");
            resp.setContentType("application/octet-stream");
            resp.setHeader("Content-Disposition", "attachment;filename=" + fileNameCN);
            resp.setContentLength((int) file.length());
            //打开指定文件流
            //这是字节流的方式
            BufferedInputStream fileInputStream = new BufferedInputStream(new FileInputStream(path));
            //规定输出流信息
            byte [] bytes = new byte[1024];
            OutputStream os = resp.getOutputStream();
            BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(os);
            while(fileInputStream.read(bytes)!=-1){
                bufferedOutputStream.write(bytes);
            }
            //关闭输入输出流
            fileInputStream.close();
            bufferedOutputStream.close();
            os.flush();
            os.close();
        }
    }

    /**
     * @Description: 查询年度报告图片数据
     * @Title: selectYearImgData
     * @return: Object
     * @author: zhangdc
     * @date:2022-10-18 10:09:15
     */
    @RequestMapping("selectYearImgData")
    @ResponseBody
    public Object selectYearImgData(@RequestParam Map<String, Object> params) {
        return analysisReportService.selectYearImgData(params);
    }

    /**
     * @Description: 查询季度报告图片数据
     * @Title: selectQuarterImgData
     * @return: Object
     * @author: sunyj
     * @date:2024-05-18 10:09:15
     */
    @RequestMapping("selectQuarterImgData")
    @ResponseBody
    public Object selectQuarterImgData(@RequestParam Map<String, Object> params) {
        return analysisReportService.selectQuarterImgData(params);
    }

    /**
     * @Description: 查询年度报告图片数据是否缓存
     * @Title: selectYearImgData
     * @return: Object
     * @author: zhangdc
     * @date:2022-10-18 10:09:15
     */
    @RequestMapping("selectImgDataByRedis")
    @ResponseBody
    public Object selectImgDataByRedis(@RequestParam Map<String, Object> params) {
        Map<String, Object> map = new HashMap<>();
        map = analysisReportService.selectImgDataByRedis(params);
        return R.ok(map);
    }

}
